<?php

 
class CV_Popular_Widget extends WP_Widget {
	
	var $prefix;
	var $textdomain;
	
	/**
	 * Set up the widget's unique name, ID, class, description, and other options.
	 * @since 0.7
	 */
	function CV_Popular_Widget() {
		$this->prefix = 'cv';
		$this->textdomain = hybrid_get_textdomain();	
		
		$widget_ops = array('classname' => 'widget-popular', 'description' => __('Displays popular posts.', $this->textdomain) );
		$this->WP_Widget("{$this->prefix}-popular-widget", __('Popular posts', $this->textdomain), $widget_ops); 		
	}
	
	/**
	 * Outputs the widget based on the arguments input through the widget controls.
	 * @since 0.7
	 */
	function widget($args, $instance) {  
		extract( $args );
		
		echo $before_widget;

		/* If there is a title given, add it along with the $before_title and $after_title variables. */
		$title = $instance['title'];
		if ( $title) {
			$title =  apply_filters( 'widget_title',  $title, $instance, $this->id_base );
			$title = str_replace('popular', '<span>popular<span>r</span></span>', $title);
			echo $before_title . $title . $after_title;
		}
		
				
		$popular_post_num = !empty($instance['count']) ? $instance['count'] : '';	
		$order_by = $instance['order_by'];  ?>	
		
		<div class="popular-posts">
			<ul>
				<?php //rewind_posts(); ?>
				<?php cv_popular_posts($order_by, $popular_post_num); ?>
			</ul>	
		</div>	
		
		<?php
		
	   echo $after_widget;
   }
	
	/**
	 * Updates the widget control options for the particular instance of the widget.
	 * @since 0.7
	 */
	function update($new_instance, $old_instance) {                
       return $new_instance;
   }
	
	/**
	 * Displays the widget control options in the Widgets admin screen.
	 * @since 0.7
	 */
	function form($instance) {  
		$defaults = array(
			'title' => 'Popular posts',			
			'count' => '5',
			'order_by' => 'comment_count'
		);
		$instance = wp_parse_args( (array) $instance, $defaults );   
			$count = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
			$order_by = array('comment_count', 'date', 'title', 'author', 'ID', 'modified', 'rand');
		?>
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', $this->textdomain ); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'count' ); ?>">Number:</label> 
			<select class="popularcount" id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>">
				<?php foreach ( $count as $option_value ) { ?>
					<option value="<?php echo $option_value; ?>" <?php selected( $instance['count'], $option_value ); ?>><?php echo $option_value; ?></option>
				<?php } ?>
			</select>
		</p>	
			
		<p>
			<label for="<?php echo $this->get_field_id( 'order_by' ); ?>">Order by:</label> 
			<select class="popularorder" id="<?php echo $this->get_field_id( 'order_by' ); ?>" name="<?php echo $this->get_field_name( 'order_by' ); ?>">
				<?php foreach ( $order_by as $option_value ) { ?>
					<option value="<?php echo $option_value; ?>" <?php selected( $instance['order_by'], $option_value ); ?>><?php echo $option_value; ?></option>
				<?php } ?>
			</select>
		</p>		
       <?php
	}

}
 
add_action('widgets_init', 'register_cv_popular_widget');

function register_cv_popular_widget() {
	register_widget('CV_Popular_Widget');
}